<?php

class ScrapyApi extends ApiBase {

	private $redis = null;
	private $scrapyTasks = 'scrapyTasks';
	private $scrapyCookie = 'scrapyCookie-=-';
	private $cookieLen = 1;
	private $sleepPrifix = 'sleep-=-';

	/**
	 * 默认每次获取url条数
	 */
	const DEFAULT_URLNUM = 5;

	/**
	 * 默认间隔时间
	 * @var int
	 */
	const DEFAULT_SLEEPTIME = 500;

	/**
	 * 默认浏览器agents
	 * @var array
	 */
	const DEFAULT_BROWSER = [
		'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36',
		'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0',
		'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
		'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
		'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.69 Safari/537.36 QQBrowser/9.0.3100.400'
	];

	/**
	 * 默认手机agents
	 * @var array
	 */
	const DEFAULT_MOBILE = [];

	public function __construct()
	{
		$this->redis = n_api('redis')->scrapy;
	}

	/**
	 * 根据传入的taskId判断task是否存在
	 * @param $taskId
	 * @return bool
	 */
	// public function TaskExists($taskId)
	// {
	// 	$taskIds = $this->redis->hKeys($this->scrapyTasks);
	// 	return in_array($taskId, $taskIds);
	// }

	/**
	 * 获取scrapy列表在redis中的key值
	 * @return string
	 */
	// public function getScrapyTaskName()
	// {
	// 	return $this->scrapyTasks;
	// }

	/**
	 * 从redis里面获取所有task列表
	 * @param $clientIp
	 * @return array
	 */
	// public function getAllTasks($clientIp)
	// {
	// 	$rs = $this->redis->hGetAll($this->scrapyTasks);
	// 	$taskLists = [];
	// 	foreach ($rs as $key => $value) {
	// 		$tmp = json_decode($value, true);
	// 		if ($tmp['status'] == 1 && $tmp['taskClientIp'] == $clientIp) {
	// 			$tmp['taskId'] = $key;
	// 			$taskLists[] = $tmp;
	// 		}
	// 	}
	// 	return $taskLists;
	// }

	/**
	 * 添加cookie
	 * @param $taskName string 任务名
	 * @param $cookie string cookie的json
	 * @return bool
	 */
	public function addCookie($taskName, $cookie)
	{
		if (is_string($cookie)) {
			$cookieKey = $this->getScrapyCookieName($taskName);
			$this->redis->rPush($cookieKey, $cookie);
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @param $taskId
	 * @return string
	 */
	public function getScrapyCookieName($taskId)
	{
		return $this->scrapyCookie . $taskId;
	}

	/**
	 * 根据taskId从redis里面读取sleepTime，如果不存在，默认设置为500ms
	 * @param $taskId
	 * @return int
	 */
	// public function getTaskSleepTime($taskId)
	// {
	// 	$redisReturn = $this->redis->hGet($this->scrapyTasks, $taskId);
	// 	$taskData = json_decode($redisReturn, true);

	// 	if (isset($taskData['taskSleepTime']))
	// 		return $taskData['taskSleepTime'];
	// 	else
	// 		return 500;
	// }

	/**
	 * 从redis获取指定taskId的cookie
	 * @param $taskId
	 * @return array 二维数组,包含多个cookie
	 */
	public function getTaskCookies($taskName)
	{
		$key = $this->getScrapyCookieName($taskName);
		$count = 0;
		$rs = [];
		while (true) {
			$len = $this->redis->lLen($key);
			if ($len != 0 && $count < $this->cookieLen){
				$tmp = $this->redis->lpop($key);
				$count++;
				$rs[] = $tmp;
			}
			else
				return $rs;
		}
	}

	/**
	 * 将基础常见的浏览器agent返回
	 * @return array
	 */
	public function getBrowserBaseAgents()
	{
		return [
			'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36',
			'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0',
			'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
			'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
			'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.69 Safari/537.36 QQBrowser/9.0.3100.400'
		];
	}

	/**
	 * 根据指定taksId将url插入到待处理redis队列里面，插入到队尾
	 * @param $taskId
	 * @param $url
	 * @param $tag
	 * @return bool
	 */
	public function pushTaskList($taskId, $url, $tag = 'default')
	{
		$data = json_encode(['url' => $url, 'tag' => $tag]);
		return $this->redis->rPush($taskId, $data);
	}

	/**
	 * 根据指定taksId将url插入到待处理redis队列里面，插入到队头
	 * @param $taskId
	 * @param $url
	 * @param $tag
	 * @return bool
	 */
	public function pushTaskListHead($taskId, $url, $tag = 'default')
	{
		$data = json_encode(['url' => $url, 'tag' => $tag]);
		return $this->redis->lPush($taskId, $data);
	}

	public function heartbeatToMongo(array $param)
	{
		$m = new MongoClient("mongodb://10.16.43.46:27017");
		$heartbeatCollection = $m->selectDB('scrapy')->selectCollection('heartbeat');

		$param['ctime'] = time();
		$heartbeatCollection->insert($param);
	}

	public function checkHeartBeat($taskId)
	{
		$m = new MongoClient("mongodb://10.16.43.46:27017");
		$heartbeatCollection = $m->selectDB('scrapy')->selectCollection('heartbeat');

		$query = [
			'taskId' => $taskId,
			'ctime' => ['$gte' => time()]
		];

		$cursor = $heartbeatCollection->find($query);
		if ($cursor->count(true) > 0)
			echo 'true';
		else
			echo 'true';
	}

	private function getTaskLen($taskId) {
		$redisReturn = $this->redis->hGet($this->scrapyTasks, $taskId);
		$taskData = json_decode($redisReturn, true);

		if (isset($taskData['taskOnceNum']))
			return $taskData['taskOnceNum'];
		else
			return 5;
	}

	public function getTaskDetail($taskId) {
		$taskLen = $this->getTaskLen($taskId);
		$count = 0;
		$rs = [];
		while (true) {
			$len = $this->redis->lLen($taskId);
			if ($len != 0 && $count < $taskLen){
				$tmp = $this->redis->lpop($taskId);
				$count++;
				$rs[] = json_decode($tmp, true);
			}
			else
				return $rs;
		}
	}
}